package com.ruoyi.employer.service.impl;

import java.util.List;

import com.ruoyi.employer.domain.PayBalance;
import com.ruoyi.employer.mapper.CargoUserMapper;
import com.ruoyi.employer.mapper.PayBalanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.employer.mapper.PayBankCardsMapper;
import com.ruoyi.employer.domain.PayBankCards;
import com.ruoyi.employer.service.IPayBankCardsService;

/**
 * 货主银行卡管理Service业务层处理
 *
 * @author ysq
 * @date 2024-08-14
 */
@Service
public class PayBankCardsServiceImpl implements IPayBankCardsService
{
    @Autowired
    private CargoUserMapper cargoUserMapper;
    @Autowired
    private PayBankCardsMapper payBankCardsMapper;
    @Autowired
    private PayBalanceMapper payBalanceMapper;

    /**
     * 查询货主银行卡管理
     *
     * @param id 货主银行卡管理主键
     * @return 货主银行卡管理
     */
    @Override
    public PayBankCards selectPayBankCardsById(Long id)
    {
        return payBankCardsMapper.selectPayBankCardsById(id);
    }

    /**
     * 查询货主银行卡管理列表
     *
     * @param payBankCards 货主银行卡管理
     * @return 货主银行卡管理
     */
    @Override
    public List<PayBankCards> selectPayBankCardsList(PayBankCards payBankCards)
    {
        return payBankCardsMapper.selectPayBankCardsList(payBankCards);
    }

    /**
     * 新增货主银行卡管理
     *
     * @param payBankCards 货主银行卡管理
     * @return 结果
     */
    @Override
    public int insertPayBankCards(PayBankCards payBankCards)
    {
        return payBankCardsMapper.insertPayBankCards(payBankCards);
    }

    /**
     * 修改货主银行卡管理
     *
     * @param payBankCards 货主银行卡管理
     * @return 结果
     */
    @Override
    public int updatePayBankCards(PayBankCards payBankCards)
    {
        return payBankCardsMapper.updatePayBankCards(payBankCards);
    }

    /**
     * 批量删除货主银行卡管理
     *
     * @param ids 需要删除的货主银行卡管理主键
     * @return 结果
     */
    @Override
    public int deletePayBankCardsByIds(Long[] ids)
    {
        return payBankCardsMapper.deletePayBankCardsByIds(ids);
    }

    /**
     * 删除货主银行卡管理信息
     *
     * @param id 货主银行卡管理主键
     * @return 结果
     */
    @Override
    public int deletePayBankCardsById(Long id)
    {
        return payBankCardsMapper.deletePayBankCardsById(id);
    }

    @Override
    public List<PayBankCards> selectByCargoUserId(Long cargoUserId) {
        return payBankCardsMapper.selectByCargoUserId(cargoUserId);
    }
    @Override
    public boolean transfer(Long fromBankCardNumber, Long toCargoUserId, Double amount) {
        PayBankCards fromAccount = payBankCardsMapper.getPayBankCardsByNumber(fromBankCardNumber);
        PayBalance toBalance = payBalanceMapper.selectByCarGoUserId(toCargoUserId);

        if (fromAccount == null) {
            throw new RuntimeException("银行卡账户不存在");
        }
        if (toBalance == null) {
            throw new RuntimeException("目标用户余额账户不存在");
        }
        if (fromAccount.getBankWallet() < amount) {
            throw new RuntimeException("银行卡余额不足");
        }

        // 扣减银行卡余额
        fromAccount.setBankWallet(fromAccount.getBankWallet() + amount);
        payBankCardsMapper.updatePayBankCards(fromAccount);

        // 增加余额账户余额
        toBalance.setBalanceWallet(toBalance.getBalanceWallet() - amount);
        payBalanceMapper.updatePayBalance(toBalance);

        return true;
    }
}
